#include <stdio.h>
#include <string.h>
#include "includes\ca51f_config.h"
#include "includes\ca51f2sfr.h"
#include "includes\ca51f2xsfr.h"
#include "includes\gpiodef_f2.h"

#include "Library\includes\delay.h"
#include "Library\includes\uart.h"
#include "Library\includes\flash.h"
#include "Library\includes\system_clock.h"
#include "includes\system.h"
#include "bsp_flash.h"
#include "bsp_lcd.h"
#include "../Inc/ebyte.h"

extern uint8_t xdata setaddr_flag;
extern uint8_t xdata setaddr_ack;

uint8_t xdata first_para = 1;
loraAddr_t xdata Myaddr = { 0 };
loraAddr_t xdata Readdr = { 0 };

void Ebyte_Port_Init(void)
{
	P51F = OUTPUT;
	P52F = OUTPUT;
	P43F = INPUT;

	// Flash_Read_Bytes((uint8_t *)&Readdr,3,Flash_Re_Addr);
}

void Ebyet_Set_Address(void)
{
	M0_SET;
	M1_SET;
	mx_delay_ms(2);
	
	while(!P43){};
	memset(stupanel.lora_tx_buf,0,sizeof(stupanel.lora_tx_buf));
	stupanel.lora_tx_buf[0] = 0xC0;
	stupanel.lora_tx_buf[1] = 0x00;
	stupanel.lora_tx_buf[2] = 0x06;
	stupanel.lora_tx_buf[3] = Myaddr.haddr;
	stupanel.lora_tx_buf[4] = Myaddr.laddr;
	stupanel.lora_tx_buf[5] = 0x61;
	stupanel.lora_tx_buf[6] = 0x00;
	stupanel.lora_tx_buf[7] = Myaddr.CH;
	stupanel.lora_tx_buf[8] = 0xC0;
	mx_delay_ms(100);
	Uart_Sendbytes(stupanel.lora_tx_buf,9);
	
	mx_delay_ms(100);
	M0_RESET;
	M1_RESET;
}	

void Ebyte_Init(void)
{
	Pcdebug("ebyte init\n");
	M0_SET;
	M1_SET;
	while(!P43){};
	Delay_ms(2);
	
	// memset(stupanel.lora_tx_buf,0,sizeof(stupanel.lora_tx_buf));
	stupanel.lora_tx_buf[0] = 0xC0;
	stupanel.lora_tx_buf[1] = 0x00;
	stupanel.lora_tx_buf[2] = 0x06;
	stupanel.lora_tx_buf[3] = Myaddr.haddr;
	stupanel.lora_tx_buf[4] = Myaddr.laddr;
	stupanel.lora_tx_buf[5] = 0x61;
	stupanel.lora_tx_buf[6] = 0x00;
	stupanel.lora_tx_buf[7] = Myaddr.CH;
	stupanel.lora_tx_buf[8] = 0x40;
	Delay_ms(50);
	Uart_Sendbytes(stupanel.lora_tx_buf,9);
	
	Delay_ms(50);
	M0_RESET;
	M1_RESET;

//   stupanel.lora_tx_buf[0] = 0xC1;
//   stupanel.lora_tx_buf[1] = 0x00;
//   stupanel.lora_tx_buf[2] = 0x06;
// 	// M0_SET;
// 	// M1_SET;
// 	Delay_ms(100);
// 	Uart_Sendbytes(stupanel.lora_tx_buf,3);
// 	Delay_ms(100);
// 	M0_RESET;
// 	M1_RESET;
}

void Ebyet_Sleep(void)
{
	M0_SET;
  	M1_SET;
}

void Ebyet_WOR(void)
{
	M0_SET;
	M1_RESET;
	while(!P43){};
}

void Ebyet_Wakeup(void)
{
	M0_RESET;
	M1_RESET;
	while(!P43){};
}

void Uart1_Recv_Handle(void)
{
	uint8_t xdata recvbuf[64];
	uint8_t xdata crc_data[32];
	uint8_t xdata cnt,xdata i;
	uint16_t xdata Crc;
	if(stupanel.uart1_rx_flag == UART_RX_DONE)
	{
		Pcdebug("rxdone\n");
		stupanel.uart1_rx_flag = UART_RX_IDLE;
		cnt = uart1_rev.head & 0x00FF;
		// uart_printf("recv data\n");
		// for(i=0;i<cnt;i++)
        // {
		// 	uart_printf("%x ",recvbuf[i]);
		// }
		// Pcdebug("\n");
		memcpy(recvbuf,uart1_rx_buf,cnt);
		if(recvbuf[0] == 0xA0)
		{
			// Pcdebug("lora recv data\r\n");
			memcpy(crc_data,&recvbuf[2],recvbuf[2]+1);
			
			Crc = usMBCRC16(crc_data,recvbuf[2]+1);
			if(Crc == (recvbuf[3+recvbuf[2]] << 8) + recvbuf[4+recvbuf[2]])
			{
				// Pcdebug("crc true\r\n");
				switch(recvbuf[1])
				{
					case SETTMP:
					{
						stupanel.Target_Temp = recvbuf[3];
						Readdr.haddr = recvbuf[4] << 8;
						Readdr.laddr = recvbuf[5];
						Readdr.CH = recvbuf[6];
					}
					break;
					case SETADDR:
					{
						Pcdebug("setaddr \n");
						Myaddr.haddr = recvbuf[3];
						Myaddr.laddr = recvbuf[4];
						Myaddr.CH = recvbuf[5];
						// Uart0_PutChar(Myaddr.haddr);
						// Uart0_PutChar(Myaddr.laddr);
						// Uart0_PutChar(Myaddr.CH);
						Flash_Write_LoraAddr(FLASH_MY_ADDR,&Myaddr);
						Readdr.haddr = recvbuf[6];
						Readdr.laddr = recvbuf[7];
						Readdr.CH = recvbuf[8];
						// Uart0_PutChar(Readdr.haddr);
						// Uart0_PutChar(Readdr.laddr);
						// Uart0_PutChar(Readdr.CH);
						// Uart0_PutChar('\n');
						Flash_Write_LoraAddr(FLASH_RE_ADDR,&Readdr);
						
						setaddr_flag = true;
						// mx_delay_ms(1000);
						
					}
					break;
					case HEARTBEAT:
					{
						loraAddr_t addr;
						Pcdebug("Heart beat\r\n");
						lcd_display_func(FUNC_CONNECT);
						addr.haddr = recvbuf[3];
						addr.laddr = recvbuf[4];
						addr.CH = recvbuf[5];
						if(addr.haddr != Readdr.haddr || addr.laddr != Readdr.laddr || addr.CH != Readdr.CH)
						{
							Readdr.haddr = addr.haddr;
							Readdr.laddr = addr.laddr;
							Readdr.CH = addr.CH;
							Flash_Write_LoraAddr(FLASH_RE_ADDR,&Readdr);
						}
						stupanel.connect_timeout = HEARTBEAT_TIMEOUT;
						Tran_Order(HEARTBEAT_ACK);
					}
					break;
				}
			}
		}
		else if(recvbuf[0] == 0xC1)
		{
			Pcdebug("ebyte return is ok\r\n");
			if(recvbuf[1] == 0x00 && recvbuf[2] == 0x06)
			{
				if(first_para)
				{
					first_para = 0;
					Myaddr.haddr = recvbuf[3];
					Myaddr.laddr = recvbuf[4];
					Myaddr.CH = recvbuf[7];
					Uart0_PutChar(Myaddr.haddr);
					Uart0_PutChar(Myaddr.laddr);
					Uart0_PutChar(Myaddr.CH);
				}
				else
				{
					Myaddr.haddr = recvbuf[3];
					Myaddr.laddr = recvbuf[4];
					Myaddr.CH = recvbuf[7];
						// Uart0_PutChar(Myaddr.haddr);
						// Uart0_PutChar(Myaddr.laddr);
						// Uart0_PutChar(Myaddr.CH);
					Flash_Write_LoraAddr(FLASH_MY_ADDR,&Myaddr);
					setaddr_ack = true;
					// Tran_Order(SETADDR_ACK);
				}
			}
		}
		memset(recvbuf, 0, cnt);
		memset(uart1_rx_buf, 0, uart1_rev.head);
		uart1_rev.head = 0;
	}
}


void Tran_Order(uint8_t header)
{
	uint8_t Crcdata[32];
	uint16_t xdata CRC;
	switch(header)
	{
		case SETTMP:
		{
			stupanel.lora_tx_buf[0] = Readdr.haddr;
			stupanel.lora_tx_buf[1] = Readdr.laddr;
			stupanel.lora_tx_buf[2] = Readdr.CH;
			stupanel.lora_tx_buf[3] = 0xA0; 
			stupanel.lora_tx_buf[4] = SETTMP;
			stupanel.lora_tx_buf[5] = 0x04;
			stupanel.lora_tx_buf[6] = stupanel.Target_Temp;
			stupanel.lora_tx_buf[7] = Myaddr.haddr;
			stupanel.lora_tx_buf[8] = Myaddr.laddr;
			stupanel.lora_tx_buf[9] = Myaddr.CH;
			
			memcpy(&Crcdata,(uint8_t*)&stupanel.lora_tx_buf[5],stupanel.lora_tx_buf[5]+1);
			CRC = usMBCRC16(Crcdata,stupanel.lora_tx_buf[5]+1);

			stupanel.lora_tx_buf[10] = CRC >> 8;
			stupanel.lora_tx_buf[11] = CRC & 0xFF;
			stupanel.lora_tx_buf[12] = 0xB0;
			mx_delay_100us(5);
			Uart_Sendbytes(stupanel.lora_tx_buf,13);
			mx_delay_100us(1000);
			Uart_Sendbytes(stupanel.lora_tx_buf,13);
		}
		break;
		case SETADDR_ACK:
	 	{
			Pcdebug("setaddr ack\n");
			stupanel.lora_tx_buf[0] = Readdr.haddr;
			stupanel.lora_tx_buf[1] = Readdr.laddr;
			stupanel.lora_tx_buf[2] = Readdr.CH;

			Uart0_PutChar(stupanel.lora_tx_buf[0]);
			Uart0_PutChar(stupanel.lora_tx_buf[1]);
			Uart0_PutChar(stupanel.lora_tx_buf[2]);
			// stupanel.lora_tx_buf[0] = 0x00;
			// stupanel.lora_tx_buf[1] = 0x00;
			// stupanel.lora_tx_buf[2] = 0x17;

			stupanel.lora_tx_buf[3] = 0xA0; 
			stupanel.lora_tx_buf[4] = SETADDR_ACK;
			stupanel.lora_tx_buf[5] = 0x04;
			stupanel.lora_tx_buf[6] = 0x01;
			stupanel.lora_tx_buf[7] = Myaddr.haddr;
			stupanel.lora_tx_buf[8] = Myaddr.laddr;
			stupanel.lora_tx_buf[9] = Myaddr.CH;
			
			memcpy(&Crcdata,(uint8_t*)&stupanel.lora_tx_buf[5],stupanel.lora_tx_buf[5]+1);
			CRC = usMBCRC16(Crcdata,stupanel.lora_tx_buf[5]+1);

			stupanel.lora_tx_buf[10] = CRC >> 8;
			stupanel.lora_tx_buf[11] = CRC & 0xFF;
			stupanel.lora_tx_buf[12] = 0xB0;
			mx_delay_100us(5);
			Uart_Sendbytes(stupanel.lora_tx_buf,13);
		}	
		break;
		case HEARTBEAT_ACK:
	 	{
			stupanel.lora_tx_buf[0] = Readdr.haddr;
			stupanel.lora_tx_buf[1] = Readdr.laddr;
			stupanel.lora_tx_buf[2] = Readdr.CH;

			stupanel.lora_tx_buf[3] = 0xA0; 
			stupanel.lora_tx_buf[4] = HEARTBEAT_ACK;
			stupanel.lora_tx_buf[5] = 0x07;
			stupanel.lora_tx_buf[6] = stupanel.Env_Temp & 0xFF;
			stupanel.lora_tx_buf[7] = stupanel.Env_Temp >> 8;
			stupanel.lora_tx_buf[8] = (stupanel.Target_Temp * 10) & 0xFF;
			stupanel.lora_tx_buf[9] = (stupanel.Target_Temp * 10) >> 8;
			stupanel.lora_tx_buf[10] = Myaddr.haddr;
			stupanel.lora_tx_buf[11] = Myaddr.laddr;
			stupanel.lora_tx_buf[12] = Myaddr.CH;

			memcpy(&Crcdata,(uint8_t*)&stupanel.lora_tx_buf[5],stupanel.lora_tx_buf[5]+1);
			CRC = usMBCRC16(Crcdata,stupanel.lora_tx_buf[5]+1);

			stupanel.lora_tx_buf[13] = CRC >> 8;
			stupanel.lora_tx_buf[14] = CRC & 0xFF;
			stupanel.lora_tx_buf[15] = 0xB0;
			mx_delay_100us(5);
			Uart_Sendbytes(stupanel.lora_tx_buf,16);
	  }	
	  break;
	}
	memset(stupanel.lora_tx_buf,0,sizeof(stupanel.lora_tx_buf));
	// memset(USART1.RxBuf,0,sizeof(USART1.RxBuf));
}


unsigned short usMBCRC16(unsigned char *pu8Buf, unsigned char u8Len) 
{
	unsigned short u16Crc = 0xFFFF;
	unsigned char u8Index = 0;
	unsigned char u8Cnt = 0;
	unsigned char u8CrcH= 0 ;
	unsigned char u8CrcL= 0 ;
	for(u8Index = 0;u8Index < u8Len; u8Index++)
	{
		u8CrcH = (unsigned char)((u16Crc & 0xFF00) >> 8);
		u8CrcL = (unsigned char)((u16Crc & 0x00FF));
		u8CrcL = u8CrcL ^ pu8Buf[u8Index];
		u16Crc = ((unsigned short)u8CrcH) << 8 | u8CrcL;
		for(u8Cnt = 0; u8Cnt < 8; u8Cnt++ )
		{
			if((u16Crc & 0x0001) == 0x0001)
			{
				u16Crc >>= 1;
				u16Crc = u16Crc ^ 0xa001; 
			}
			else
			{
				u16Crc >>= 1; 
			}
		}
	}
	return u16Crc; 
}

